package br.com.gerador.component;

import java.awt.Component;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import java.util.List;

import br.com.gerador.icomponent.IGComponent;
import br.com.gerador.icomponent.IGPage;
import br.com.gerador.icomponent.IGProperty;
import br.com.gerador.icomponent.IGProprierdade;

abstract public class GComponent extends Resizable implements IGComponent,IGProprierdade{
	private IGPage page;
	
	public GComponent() {
		addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if( e.getKeyCode() == KeyEvent.VK_DELETE ){
					GPage panel = (GPage)((GComponent)e.getSource()).getParent();
					panel.remove((Component)e.getSource());
					panel.repaint();
				}else if( e.isControlDown() && e.getKeyCode() == KeyEvent.VK_C ){
					copiarComponente();
				}else if( e.isControlDown() && e.getKeyCode() == KeyEvent.VK_V ){
					colarComponente();
				}else if( e.getKeyCode() == KeyEvent.VK_DOWN ){					
					if( e.isShiftDown() ){
						movePosition( 0,5);
					}else{
						movePosition( 0,1);
					}
				}else if( e.getKeyCode() == KeyEvent.VK_UP ){
					if( e.isShiftDown() ){
						movePosition( 0,-5);
					}else{
						movePosition( 0,-1);
					}
				}else if( e.getKeyCode() == KeyEvent.VK_LEFT ){
					if( e.isShiftDown() ){
						movePosition( -5,0);
					}else{
						movePosition( -1,0);
					}
				}else if( e.getKeyCode() == KeyEvent.VK_RIGHT ){
					if( e.isShiftDown() ){
						movePosition(5,0);
					}else{
						movePosition(1,0);
					}
				}
				super.keyPressed(e);
			}
		});
		addMouseListener(mouseAdapter);
	}

	private void copiarComponente(){
		page.getPrincipal().copyComponentInPage(this);
	}
	
	private void colarComponente(){
		page.getPrincipal().pasteComponentInPage();
	}

	private void movePosition(int x, int y){
         Rectangle bounds = getBounds();
         bounds.translate(x, y);
         setBounds(bounds);
		 resize();
	}
	
	public void initComponent(int x, int y){
	    setBounds(x, y, getDefaultW(), getDefaultH());
	}

	public IGPage getPage() {
		return page;
	}

	public void setPage(IGPage page) {
		this.page = page;
	}

	public void montaPropriedades(){
		page.getPrincipal().getPropriedades().setComponent(this);
		page.getPrincipal().getPropriedades().bind();
	}

	MouseAdapter mouseAdapter = new MouseAdapter() {
		@Override
		public void mouseClicked(MouseEvent e) {
			montaPropriedades();
			super.mouseClicked(e);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			page.getPrincipal().createComponentInPage(e.getX(), e.getY());
		}
	};
	

	public void id(String value){
		
	}
	@Override
	public void firePropriedade(String method,Object value){
		try {
			Method m = this.getClass().getMethod(method, new Class[]{String.class});
			m.invoke(this, value);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void bindPropriedades() {
		for( String[] p : getPropriedades() ){
			if( p[2] != null ){
				firePropriedade(p[2], p[1]);
			}
		}
	}
	
	@Override
	public String[][] getPropriedades(){
		return new String[][]{};
	}	
}
